<?php
class EditController extends BaseController {

	public function actionIndex(){
		echo 'Edit index';
	}
	
	public function actionTree(){
		echo 'Edit tree';
		//$this->php = obj('Tree')->tree['php'];
		//$this->cpp = obj('Tree')->tree['cpp'];
		$this->json = obj('Tree')->json_all();
		
		//var_dump($this->json);
		$this->display('edit_tree.tpl');
	}
	
	public function actionCourse(){
		if(!empty($_POST['op']) && 'course_edit' == $_POST['op'] && obj('Book')->power()) { //提交处理
			$node_path = $this->arg('node_path');
			$title = htmlspecialchars($this->arg('title'));
			$content = preg_replace('#<script(.*?)</script>#is','',$this->arg('content'));//去js 留html
			$time = empty($_POST['time'])? time() : strtotime($_POST['time']);
			
			$user_id = obj('User')->is_login();
			$node = obj('Tree')->find(array('node_path'=>$node_path),'node_note');
			$uri = empty($node['node_note'])?'':$node['node_note'];
			
			obj('Course')->insert(array('node_path'=>$node_path,'title'=>$title,
				'content'=>$content,'cr_time'=>$time,'user_id'=>$user_id,'uri'=>$uri));
			
		}else {
			$this->i = $this->arg('i');
			$this->items = obj('Define')->get_items();
			$this->tree = obj('Tree')->getPathByItem($this->i);
			$this->display('edit_course.tpl');
		}
	}
	
	//$this->display('func_detail.tpl');
	
	
	
	
	
	
	
	//基本完成的:
	public function actionFunc(){
		if(!obj('Func')->power()) $this->alert('你无此权限!',URL);
		if(!empty($_POST['op']) && 'func_edit' == $_POST['op']) {
			//var_dump($_POST);
			$item_name = htmlspecialchars($this->arg('item_name')); //去html标签
			$func_name = htmlspecialchars($this->arg('func_name'));
			$func_note = htmlspecialchars($this->arg('func_note'));
			$content = preg_replace('#<script(.*?)</script>#is','',$this->arg('content'));//去js 留html
			$content_en = preg_replace('#<script(.*?)</script>#is','',$this->arg('content_en'));
			$time = empty($_POST['time'])? time() : strtotime($_POST['time']);
			$user_id = obj('User')->is_login();
			
			$re = obj('Func')->find(array('item_name'=>$item_name,'func_name'=>$func_name),'id');
			if(empty($re['id'])) {//无此记录 新增
				obj('Func')->insert(array('item_name'=>$item_name,'func_name'=>$func_name,'func_note'=>$func_note,'content'=>$content,
				'content_en'=>$content_en,'cr_time'=>$time,'user_id'=>$user_id,'flag'=>1));
			}else {
				obj('Func')->update(array('id'=>$re['id']),array('func_note'=>$func_note,'content'=>$content,'content_en'=>$content_en,'cr_time'=>$time,'flag'=>1));
			}
			$this->to(URL."$item_name/func/$func_name");
			
		}else{
			$this->items = obj('Define')->get_items();
			$this->pre_item = (!empty($_GET['i']) && in_array($_GET['i'],$this->items))?$_GET['i']:'';
			$this->pre_func = empty($_GET['f'])?'':$_GET['f'];
			$this->display('edit_func.tpl');
		}
		
		//$this->display('func_detail.tpl');
	}
	
	public function actionBook(){
		if(!empty($_POST['op']) && 'book_edit' == $_POST['op'] && obj('Book')->power()) {
			$flag = 1;//SAFE:前期默认直接显示, 可以临时改为0,禁止直接显示
			$book_id = (int)$this->arg('book_id');
			$id = (int)$this->arg('id');
			$uid = obj('User')->is_login();
			
			$func_name = htmlspecialchars($this->arg('func_name')); //去html标签
			$title = htmlspecialchars($this->arg('title'));
			$title_en = htmlspecialchars($this->arg('title_en'));
			$content = preg_replace('#<script(.*?)</script>#is','',$this->arg('content'));//去js 留html
			$content_en = preg_replace('#<script(.*?)</script>#is','',$this->arg('content_en'));
			$time = empty($_POST['time'])? time() : strtotime($_POST['time']);
			
			$section = obj('Book')->find(array('book_id'=>$book_id,'section_id'=>$id),'id');
			if(!empty($section)) {//无则允许插入 TODO:未来修改
				obj('Book')->update(array('section_id'=>$id,'book_id'=>$book_id),array('func_name'=>$func_name,'title'=>$title,'title_en'=>$title_en,'content'=>$content,'content_en'=>$content_en,'cr_time'=>$time,'flag'=>$flag,'user_id'=>$uid));
			}
			$this->to(URL."book/$book_id/$id");
		}else {
			$id = (int) $this->arg('s');//s b 和post区分
			$book_id = (int) $this->arg('b');
			$this->section = obj('Book')->find(array('section_id'=>$id,'book_id'=>$book_id));
			$this->book = obj('Book')->book_define[$book_id];
			$this->display('edit_book.tpl');
		}
	}
	
}